#include<string>
using namespace std;
class Solution {
public:
    bool patternMatching(string pattern, string value) {
        if (pattern.length() < 1) return value.length();
        if (pattern.length() == 1 && value.length() < 1) return true;
        int na = 0, nb = 0;
        for (int i = 0; i < pattern.length(); ++i) {
            if (pattern[i] == 'a') ++na;
            else ++nb;
        }
        int len = value.length();
        for (int la = 0; la <= len; ++la) {
            if (nb != 0 && (len - la * na) % nb != 0)
                continue;
            int lb = 0;
            if (nb != 0) lb = (len - la * na) / nb;
            int i = 0, j = 0;
            string str_a = "";
            string str_b = "";
            while (i < pattern.length() && j < value.length()) {
                if (pattern[i] == 'a') {
                    if (str_a == "")
                        str_a = value.substr(j, la);
                    else {
                        bool flag = true;
                        for (int k = 0; k < la; ++k) {
                            if (str_a[k] != value[j + k]) {
                                flag = false;
                                break;
                            }
                        }
                        if (!flag) break;
                    }
                    j += la;
                }
                else {
                    if (str_b == "")
                        str_b = value.substr(j, lb);
                    else {
                        bool flag = true;
                        for (int k = 0; k < lb; ++k) {
                            if (str_b[k] != value[j + k]) {
                                flag = false;
                                break;
                            }
                        }
                        if (!flag) break;
                    }
                    j += lb;
                }
                ++i;
            }
            if (i == pattern.length() && j == value.length())
                return true;
        }
        return false;
    }
};

int main() {
    Solution sol;
    sol.patternMatching("bbb", "xxxxxx");
}